import IP2Region, { IP2RegionResult } from "ip2region";

/**
 * ip2region 工具类
 */
export class Ip2RegionUtils {
    private static readonly ip2 = new IP2Region({ disableIpv6: true }); // 关闭 ipv6 查询功能（减少内存占用）

    /**
     * 获取 ip 地理位置信息
     * @param ip
     * @example
     * 218.4.167.70 => { country: '中国', province: '江苏省', city: '苏州市', isp: '电信' }
     * localhost => null
     * 127.0.0.1 => { country: '', province: '', city: '内网IP', isp: '内网IP' }
     * ::1 => null (::1 是 IPv6 的本地回环地址（localhost），相当于 IPv4 中的 127.0.0.1)
     * 开启 ipv6 查询：::1 => { country: '', province: '', city: '', isp: '本机地址' }
     * 192.168.0.1 => { country: '', province: '', city: '内网IP', isp: '内网IP' }
     */
    static async getIpRegionInfo(ip: string): Promise<IP2RegionResult | null> {
        return await this.ip2.search(ip);
    }
}

// Ip2RegionUtils.getIpRegionInfo("::1").then(res => console.log(res));
